﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MakeTableTool.Template
{
    /// <summary>
    /// 模板生成工具
    /// </summary>
    public class TemplateGenTool
    {
        //[Type]<->[FileString]
        public static Dictionary<string,string> templateFileDic = new Dictionary<string, string>();
        //public const string Tag_Begin = "[begin]";
        //public const string Tag_End = "[end]";

        //匹配[begin][type][end]
        private const string regex = @"\[begin\](\[.*?\])([\s\S]*?)\[end\]";

        public static bool isInitTemplateFile { get; private set; }
        /// <summary>
        /// 获取模板文件
        /// </summary>
        /// <returns></returns>
        public static void InitTemplateFile(string filePath)
        {
            templateFileDic.Clear();
            var templateString = File.ReadAllText(filePath);
            var matches = Regex.Matches(templateString, regex);
            for (int i = 0; i < matches.Count; i++)
            {
                var mc = matches[i];
                var type = mc.Groups[1].Value;
                var rawString = mc.Groups[2].Value;
                if (!templateFileDic.ContainsKey(type))
                {
                    templateFileDic.Add(type, rawString);
                }
            }

            if(string.IsNullOrEmpty( GetTemplate("[Templete]")))
            {
                throw new Exception("模板文件内容不正确,没有Template");
            }
            isInitTemplateFile = true;
        }

        /// <summary>
        /// 生成模板文件
        /// </summary>
        /// <param name="excelTable"></param>
        /// <param name="savingPath"></param>
        public static void GenTemplateFile(ExcelTable excelTable,string savingPath)
        {
            if (isInitTemplateFile == false)
                return;
            TemplateCodeGen codeGen = new TemplateCodeGen(excelTable);
            codeGen.GenTemplateFile(savingPath);
        }



        public static string GetTemplate(string type)
        {
            if (templateFileDic.ContainsKey(type))
                return templateFileDic[type];

            return "";
        }


      

        public static string ReplaceMulitply(string srcString,Dictionary<string,string> replaceDic)
        {
            foreach (var replaceSetting in replaceDic)
            {
                srcString = srcString.Replace("%" + replaceSetting.Key + "%", replaceSetting.Value);
            }

            return srcString;
        }


        /// <summary>
        /// 将第一位变为大写
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static string CapitalizeFirstLetter(string original)
        {
            if ((original == null) || (original.Length == 0))
            {
                return original;
            }
            return original.Substring(0,1).ToUpper() + original.Substring(1);
        }
    }
}
